import { _decorator, Component, game, math, Node, v2, v3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('levelBgpng')
export class levelBgpng extends Component {

    //四个碎片的位置(x,y,反转，角度)
    polygonsInfo :polygonInfo[] = [
        {x:42,y:-260,angle:[180,-180],reverse:-1},//金黄
        {x:384,y:-260,angle:[180,-180],reverse:-1},//紫色
        {x:-360,y:-260,angle:[0,360],reverse:1},//橙黄
        {x:465,y:-260,angle:[90,-270],reverse:1},//绿色
           
    ]


    //绘制时四个顶点的位置
    positions :math.Vec3[] = [
        v3(0,0,0),
        v3(512,0),
        v3(512,104,0),
        v3(0,104,0)
    ]


    protected start(): void {
        //注册监听
        game.on('gameVictory',this.gameVictory,this)
    }

    gameVictory() {
        const childrens = this.node.children;

        let flag = 0;
        for(let i=0;i<childrens.length;i++){
            if(childrens[i].scale.x != this.polygonsInfo[i].reverse){
                console.log("反转不相等")
                break
            }else if((childrens[i].angle != this.polygonsInfo[i].angle[1]) || (childrens[i].angle != this.polygonsInfo[i].angle[2])){
                console.log("角度不相等")
                break
            }else if(childrens[i].getPosition().x != this.polygonsInfo[i].x){
                console.log("x坐标不相等")
                break
            }else if(childrens[i].getPosition().y != this.polygonsInfo[i].y){
                console.log("y坐标不相等")
                break
            }else{
                flag++
            }
            
        }

        if(flag==4){
            console.log("拼图成功！")
        }
        
    }

    protected onDestroy(): void {
        game.off('gameVictory',this.gameVictory,this)
    }



}


interface polygonInfo{
    x: number;
    y: number;
    angle :number[];
    reverse :number;
}




